<?php
  
class ArrayGridBase {

	protected $Data;
	protected $title;
	protected $select;	   //@array 'title'=>选择域显示名称，
	protected $act_title;
	protected $act_list;
	protected $act_key;
	protected $check;        /*是否显示 check button, 显示时，$check等于 字段名， hidden input的值等于此字段值，＝$select['key']*/
	protected $pageline;
	protected $cond;//对应栏位的取值范围
	protected $cpage;
	
	protected function showPager($nPage)
	{
		$o='';
		
    	//$action=$_SERVER['REQUEST_URI'];
		//$n=strpos($action,'?');
		//if ($n!=false)	$action=substr(0,$n);
		
    	/* add pager*/
    	if ($nPage>1) {
    		$o=$o.'共'.$nPage.'页 转到第';
	    	for($i=0; $i<$nPage; $i++) {
	    		if ($i == $this->cpage) {
	    			$o=$o.'<a href="?p='.$i.'"><strong>'.($i+1).'</strong></a> ';
	    			continue;
	    		}
   	 		$o=$o.'<a href="?p='.$i.'">'.($i+1).' </a> ';
    		}
			$o=$o.'页';
    	}

		return $o;
	}
	
	protected function showTitle($row)
	{	
		$o='';
		/*显示选择区TITLE*/
		if (!empty($this->select)) $o=$o.'<th width="50px"><input type="checkbox" id="_gchk_" onClick="GridChkAll('.
															$this->pageline.')">'.$this->select['title'].'</th>';
		
		/*显示TITLE*/ 
		if (!empty($this->title)) {
			foreach($this->title as $key => $value ) {
				$o=$o."<th>$value</th>";
			}
		}else{
		 	foreach($row as $key => $value) {
 		 		$o=$o."<th>$key</th>";
 		 	}	
		}
		/*显示操作区TITLE */
		if (!empty($this->act_title)) $o=$o."<th>".$this->act_title."</th>";
		$o=$o."</tr>";
				
		return $o;
	}	

	protected function showAct($row)
	{
		$o='';
		if (!empty($this->act_title) && !empty($this->act_key)) {
			$o=$o.'<td>';
			 	foreach($this->act_list as $act) {
			 		if (isset($act['url'])) $url=$act['url'];
			 		else $url='';
			 		$url=$url.'?'.$this->act_key.'='.$row[$this->act_key];
			 		//$url=$url.'/'.$this->act_key.'/'.$row[$this->act_key];
			  	$o=$o.'<a href="'.$url.'">'.$act['title'].'</a> ';
			  	}	
			$o=$o.'</td>';
		}
		return $o;
	}	
	
//$cond = array('status'=>array('1'=>'已提交','2'=>'用户锁定','3'=>'系统锁定'));	
	protected function showLine($row,$idx,$cond = NULL)
	{
		$o='';
		$o=$o.'<tr>';
		if (!empty($this->check)) {
			$o=$o.'<td><input type="checkbox" id="_chk'.$idx.'" name="_chk'.$idx.'" >';
			$o=$o.'<input type="hidden" name="_hid'.$idx.'" value="'.$row[$this->check].'"></td>';
		}
		if (empty($cond)) {
			$cond = array();
		}
		if (!empty($this->title)) {
			foreach($this->title as $key=>$value){
//				$o=$o.'<td>'.$row[$key].'</td>';
				$res = array_key_exists($key, $cond);
				if ($res) {
					$r = array_key_exists($row[$key], $cond[$key]);
					if ($r) {
						$o=$o.'<td>'.$cond[$key][$row[$key]].'</td>';
					} else {
						$o=$o.'<td>'.$row[$key].'</td>';
					}
				} else {
					$o=$o.'<td>'.$row[$key].'</td>';
				}
			}
			$o=$o.$this->showAct($row).'</tr>';
		}else{
			foreach($row as $key=>$value) {
				$res = array_key_exists($key, $cond);
				if ($res) {
					$r = array_key_exists($row[$key], $cond[$key]);
					if ($r) {
						$o=$o.'<td>'.$cond[$key][$row[$key]].'</td>';
					} else {
						$o=$o.'<td>'.$value.'</td>';
					}
				} else {
					$o=$o.'<td>'.$value.'</td>';
				}
			}
			$o=$o.$this->showAct($row).'</tr>';
		}
		return $o;
	}
}  	
